﻿using Client;
using ManageSystem.Manager;
using Shared.Method;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ManageSystem
{
    public partial class FormManager : Form
    {
        private string _keyWord = null;
        private bool _canDist = false;

        public FormManager()
        {
            InitializeComponent();

            //MethodWcf.KEY = "RssPmq&AwYcc7<98Bux";
            _keyWord = AppSetting.GetValue("KeyWord");
        }

        private void 流程管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //this.flowLayoutPanel1.Controls.Clear();
            CloseForm();
            FormFlow flow = new FormFlow();
            flow.Show();
            this.flowLayoutPanel1.Controls.Add(flow);

            SetDefaultSize();
        }

        private void 工序管理ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            //this.flowLayoutPanel1.Controls.Clear();
            CloseForm();
            FormNode node = new FormNode();
            node.Show();
            this.flowLayoutPanel1.Controls.Add(node);
            SetDefaultSize();

        }

        private void 产线管理ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            //this.flowLayoutPanel1.Controls.Clear();
            CloseForm();
            FormLine line = new FormLine();
            line.Show();
            this.flowLayoutPanel1.Controls.Add(line);

            SetDefaultSize();
        }


        private void 员工信息管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //this.flowLayoutPanel1.Controls.Clear();
            CloseForm();
            FormWorker worker = new FormWorker();
            worker.Show();
            this.flowLayoutPanel1.Controls.Add(worker);
            SetDefaultSize();

        }

        private void 工位管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //this.flowLayoutPanel1.Controls.Clear();
            CloseForm();
            FormStation staForm = new FormStation();
            staForm.Show();
            this.flowLayoutPanel1.Controls.Add(staForm);
            SetDefaultSize();
        }

        //private void 分配SN号ToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    //this.flowLayoutPanel1.Controls.Clear();
        //    CloseForm();
        //    FormDistributeSn distSn = new FormDistributeSn();
        //    distSn.Show();
        //    this.flowLayoutPanel1.Controls.Add(distSn);
        //    SetDefaultSize();
        //}

        private void MAC导入ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            //this.flowLayoutPanel1.Controls.Clear();
            CloseForm();
            FormInputData inputData = new FormInputData(0);
            inputData.Show();
            this.flowLayoutPanel1.Controls.Add(inputData);
            SetDefaultSize();
        }

        private void 产品状态ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //产品查询；①产品当前状态②过程信息③测试日志记录④修改所在工序
            //this.flowLayoutPanel1.Controls.Clear();
            CloseForm();
            FormProduct product = new FormProduct();
            product.Show();
            this.flowLayoutPanel1.Controls.Add(product);

            SetDefaultSize();
        }

        private void bOSA导入ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //this.flowLayoutPanel1.Controls.Clear();
            CloseForm();
            FormInputData inputData = new FormInputData(1);
            inputData.Show();
            this.flowLayoutPanel1.Controls.Add(inputData);

            SetDefaultSize();
        }

        private void 产量查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //查工位的产量，按照时间计算
            //this.flowLayoutPanel1.Controls.Clear();
            CloseForm();
            FormStatistics tics = new FormStatistics();
            tics.Show();
            this.flowLayoutPanel1.Controls.Add(tics);

            SetDefaultSize();
        }

        //private void 解锁MACToolStripMenuItem_Click(object sender, EventArgs e)
        //{

        //    //this.flowLayoutPanel1.Controls.Clear();
        //    CloseForm();
        //    FormMac mac = new FormMac();
        //    mac.Show();
        //    this.flowLayoutPanel1.Controls.Add(mac);

        //    SetDefaultSize();
        //}



        private void CloseForm()
        {
            for (int i = 0; i < this.flowLayoutPanel1.Controls.Count; i++)
            {
                var control = flowLayoutPanel1.Controls[i];
                control.Dispose();
            }
            this.flowLayoutPanel1.Controls.Clear();
        }

        private void FormManager_Load(object sender, EventArgs e)
        {
            //加载软件版本
            string version = Application.ProductVersion;

            //string version = Application.ProductVersion;
            string name = Application.ProductName;

            string sqlVersionCheck = $"SELECT CONVERT(VARCHAR,COUNT(*)) FROM SOFTWARE WHERE NAME='{name}' AND VERSION='{version}'";

            var retVersionCheck = MethodWcf.ExecuteReturnSQLClient(sqlVersionCheck);

            if (retVersionCheck != "1")
            {
                MessageBox.Show("软件版本不正确，请升级到最新版本");
                Close();
                return;
            }

            //服务器网段           
            string server = AppSetting.GetValue("ServerNetwork");
            if (string.IsNullOrEmpty(server))
            {
                MessageBox.Show("未能加载服务器网段");
                Close();
                return;
            }

            string macAddress = MacClass.GetLocalMac(server);
            //MessageBox.Show(macAddress);
            this.Text = "友创达车间后台管理系统 V" + version+"_"+macAddress;


            //计算KEYWORD
            string keyWordMac = Encrypt.Encrypt3DES(macAddress);
            //MessageBox.Show(keyWordMac);

            if (string.IsNullOrEmpty(keyWordMac))
            {
                MessageBox.Show("计算验证码失败");
                Close();
                return;
            }

            //获得配置文件中的对应的KEYWORD
            string keyWordConfig = AppSetting.GetValue("KeyWord");
            //MessageBox.Show(keyWordConfig);

            if (keyWordConfig != keyWordMac)
            {
                //验证KEY失败
                this.工序管理ToolStripMenuItem.Visible = false;
                this.产线管理ToolStripMenuItem.Visible = false;
                this.员工管理ToolStripMenuItem.Visible = false;
                this.导入导出ToolStripMenuItem.Visible = false;
            }
            else
            {
                _canDist = true;
            }

            SetDefaultSize();

            this.Resize += new EventHandler(Form_Resize);
        }


        #region 自动缩放

        private void SetDefaultSize()
        {
            return;
            //等比例缩放
            X = this.Width;
            Y = this.Height;

            setTag(this);
        }

        private float X = 0.0f;
        private float Y = 0.0f;

        private void setTag(Control cons)
        {
            try
            {
                foreach (Control con in cons.Controls)
                {
                    con.Tag = con.Width + ":" + con.Height + ":" + con.Top + ":" + con.Left + ":" + con.Font.Size;
                    if (con.Controls.Count > 0)
                    {
                        setTag(con);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void setControls(float newx, float newy, Control cons)
        {
            try
            {
                foreach (Control con in cons.Controls)
                {
                    //if (string.IsNullOrEmpty(con.Tag.ToString())) continue;
                    String[] mytags = con.Tag.ToString().Split(new char[] { ':' });
                    float a = Convert.ToSingle(mytags[0]) * newx;
                    con.Width = (int)a;
                    a = Convert.ToSingle(mytags[1]) * newy;
                    con.Height = (int)a;
                    a = Convert.ToSingle(mytags[2]) * newy;
                    con.Top = (int)a;
                    a = Convert.ToSingle(mytags[3]) * newx;
                    con.Left = (int)a;
                    Single currectsize = Convert.ToSingle(mytags[4]) * newy;
                    con.Font = new Font(con.Font.Name, currectsize, con.Font.Style, con.Font.Unit);
                    if (con.Controls.Count > 0)
                    {
                        setControls(newx, newy, con);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void Form_Resize(object sender, EventArgs e)
        {
            return;
            float newX = this.Width / X;
            float newY = this.Height / Y;

            setControls(newX, newY, this);
            //this.Text = this.Width.ToString() + "*" + this.Height.ToString();
        }

        #endregion


        private void 管理订单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseForm();
            FormOrder order = new FormOrder(false,_canDist);
            order.Show();
            this.flowLayoutPanel1.Controls.Add(order);
            SetDefaultSize();
        }

        private void 管理已完成订单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseForm();
            FormOrder order = new FormOrder(true,_canDist);
            order.Show();
            this.flowLayoutPanel1.Controls.Add(order);
            SetDefaultSize();
        }

        private void 重复MAC查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseForm();
            FormProductDouble proD= new FormProductDouble();
            proD.Show();
            this.flowLayoutPanel1.Controls.Add(proD);
            SetDefaultSize();
        }

        private void 产量统计ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseForm();
            FormStatisticsOut tics = new FormStatisticsOut();
            tics.Show();
            this.flowLayoutPanel1.Controls.Add(tics);

            SetDefaultSize();
        }

        private void BOB数据管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseForm();
            FormBobData bob = new FormBobData();
            bob.Show();
            this.flowLayoutPanel1.Controls.Add(bob);

            SetDefaultSize();
        }

        //private void 分配SN号ToolStripMenuItem_Click_1(object sender, EventArgs e)
        //{
        //    CloseForm();
        //    FormDistributeSn distSn = new FormDistributeSn();
        //    distSn.Show();
        //    this.flowLayoutPanel1.Controls.Add(distSn);
        //    SetDefaultSize();
        //}
    }
}
